package com.news.web.servlet.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.service.ReviewService;
import com.news.service.impl.ReviewServiceImpl;

/**
 * Servlet implementation class AdminReviewDeleteServlet
 */
@WebServlet("/AdminReviewDeleteServlet")
public class AdminReviewDeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 获取传入数据
		String reviewIdStr = request.getParameter("reviewId");
		// 如果 值 为空或不存在，直接返回错误信息或者跳转回原页面
		if (reviewIdStr == null || reviewIdStr.isEmpty()) {
			request.setAttribute("reviewDeleteBoolean", null);
			request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		int reviewId;
		// 解析 id 成为整数
		try {
			reviewId = Integer.parseInt(reviewIdStr);
		} catch (NumberFormatException e) {
			// 如果无法解析为整数，可以记录日志或者向用户显示错误信息
			e.printStackTrace();
			request.setAttribute("reviewDeleteBoolean", null);
			request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		// 执行删除方法
		Boolean reviewDeleteBoolean = reviewService.deleteReview(reviewId);
		// 将数据存入
		request.setAttribute("reviewDeleteBoolean", reviewDeleteBoolean);
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
